package com.ops.controller;

import com.ops.entity.Inspection;
import com.ops.service.InspectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 巡检管理控制器
 * 处理巡检记录的导入和查询等操作
 */
@RestController
@RequestMapping("/api/inspections")
public class InspectionController {
    
    @Autowired
    private InspectionService inspectionService;
    
    /**
     * 导入巡检数据
     * 支持Excel文件导入巡检记录
     * 
     * @param file Excel文件
     * @param reportUuid 报告UUID
     * @param hospitalId 医院ID
     * @param demandYear 需求年份
     * @return 导入结果
     */
    @PostMapping("/import")
    public ResponseEntity<?> importInspections(
        @RequestParam("file") MultipartFile file,
        @RequestParam("reportUuid") String reportUuid,
        @RequestParam("hospitalId") String hospitalId,
        @RequestParam("demandYear") String demandYear
    ) {
        try {
            int count = inspectionService.importExcel(file, reportUuid, hospitalId, demandYear);
            Map<String, Object> response = new HashMap<>();
            response.put("count", count);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("导入失败：" + e.getMessage());
        }
    }

    /**
     * 获取巡检记录列表
     * 根据报告UUID、医院ID和年份查询巡检记录
     */
    @GetMapping("/{reportUuid}/{hospitalId}/{demandYear}")
    public ResponseEntity<List<Inspection>> getInspections(
        @PathVariable String reportUuid,
        @PathVariable String hospitalId,
        @PathVariable String demandYear
    ) {
        List<Inspection> inspections = inspectionService.getInspections(reportUuid, hospitalId, demandYear);
        return ResponseEntity.ok(inspections);
    }
} 